package com.demo2do.boyle.backend.web.controller.task;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.Constant;
import com.demo2do.boyle.backend.service.advertisement.AdvertisementService;
import com.demo2do.boyle.backend.service.advertisement.TaskService;
import com.demo2do.boyle.backend.service.customer.CustomerMessageService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.advertisement.PutType;
import com.demo2do.boyle.common.entity.advertisement.Status;
import com.demo2do.boyle.common.entity.customer.message.Type;
import com.demo2do.boyle.common.entity.system.Principal;
import com.demo2do.boyle.common.entity.task.Task;
import com.demo2do.boyle.common.entity.task.TaskStatus;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;

/**
 * 
 * @author mohanlan leo
 *
 */
@Controller
@RequestMapping("/task-audits")
@MenuSetting("menu-advertisement")
public class TaskAuditController {
	
	private final static int TASK_REWARD = 50;
	
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private CustomerMessageService customerMessageService;
	
	@Autowired
	private AdvertisementService advertisementService;
    
    /**
     * 
     * list all tasks
     * 
     * @return
     */
    @RequestMapping("")
    @MenuSetting("submenu-task-audit")
    public ModelAndView list(Page page) {
        ModelAndView modelAndView = new ModelAndView("task/audit/task-audit-list");
        
        List<Task> tasks = taskService.list(TaskStatus.DONE, page);
        int count = taskService.count(TaskStatus.DONE);
        modelAndView.addObject("tasks", tasks);
        modelAndView.addObject("count", count);
        return modelAndView;
    }
    
    /**
     * view the audit task detail
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @MenuSetting("submenu-task-audit")
    public ModelAndView view(@PathVariable("id") Integer id) {
    	
    	ModelAndView modelAndView = new ModelAndView("task/audit/task-audit-view");
    	
    	Task task = taskService.load(Task.class, id);
    	modelAndView.addObject("task", task);
    	
        return modelAndView;
    }
    
    /**
     * display the audit panel
     * 
     * @param id
     * @return the view to confirm the submit
     */
    @RequestMapping("/{id}/auditPass")
    public ModelAndView auditPass(@PathVariable("id") Integer id) {
    	ModelAndView modelAndView = new ModelAndView("task/audit/task-audit-pass");
		modelAndView.addObject("id", id);
		return modelAndView;
    }
    
    /**
     * do audit pass
     * 
     * @param id
     * @return the audit result with message
     */
    @RequestMapping(value ="/{id}/auditPass", method = RequestMethod.PATCH )
    @MenuSetting("submenu-task-audit")
    public @ResponseBody String onAuditPass(@PathVariable("id") Integer id, @Secure Principal principal) {
    	Result result = new Result();
    	
    	try{
    		
    		Task task = taskService.load(Task.class, id);
    		taskService.auditPass(task.auditPass(principal.getName()));
    		
    		customerMessageService.create(task.getCustomer(), Constant.CUSTOMER_MESSAGE_TASK_AUDITED, Type.TASK);
    		
    		Advertisement advertisement = advertisementService.load(Advertisement.class, task.getAdvertisement().getId());
    		//update claimNumber
    		advertisementService.updateClaimNumber(advertisement);
    		
    		//int countFinishNum = taskService.countFinishedNum(advertisement);
    		int countFinishNum = advertisement.getClaimNumber();
    		int taskNum = 0;
    		
    		if (advertisement.getType() == PutType.BUDGET) {
    			taskNum = (int)Math.floor(advertisement.getPutNumber()/TASK_REWARD);
			} else {
				taskNum = advertisement.getPutNumber();
			}
    		
    		if (countFinishNum == taskNum) {
				advertisement.setStatus(Status.COMPLETED);
				advertisementService.update(advertisement);
				taskService.updateOtherStatus(task);
			}
    		
    		
    		
        	result.success().message("审核成功");
        	
    	}catch(Exception e){
    		result.fail().message("审核失败");
    	}
    	return JsonUtils.toJsonString(result);
    }

    /**
     * display the audit panel
     * 
     * @param id
     * @return the view to confirm the submit
     */
    @RequestMapping("/{id}/auditUnpass")
    public ModelAndView auditUnpass(@PathVariable("id") Integer id) {
    	ModelAndView modelAndView = new ModelAndView("task/audit/task-audit-unpass");
		modelAndView.addObject("id", id);
		return modelAndView;
    }
    
    /**
     * do audit unpass
     * 
     * @param id
     * @return the audit result with message
     */
    @RequestMapping(value ="/{id}/auditUnpass", method = RequestMethod.PATCH )
    @MenuSetting("submenu-task-audit")
    public @ResponseBody String onAuditUnPass(@PathVariable("id") Integer id, @Secure Principal principal) {
    	Result result = new Result();
    	
    	try{
    		
    		Task task = taskService.load(Task.class, id);
    		taskService.auditUnpass(task.auditUnpass(principal.getName()));
    		customerMessageService.create(task.getCustomer(), Constant.CUSTOMER_MESSAGE_TASK_FAILED_AUDITED, Type.TASK);
    		
        	result.success().message("审核不通过");
    	}catch(Exception e){
    		result.fail().message("审核失败");
    	}
    	return JsonUtils.toJsonString(result);
    }
    
}
